home *** CD-ROM | disk | FTP | other *** search
- /*
- FileNation XFCN v1.0
-
- ©1991 Apple Computer, Inc.; by Mike Byrne
-
- Given a full pathname, this XFCN will go out and search for a 'vers' resource, and then
- pull the country code out of it if there is one. No problem...
-
- Form:
- FileNation(<pathName>)
-
- # the MPW 3.2 build commands:
- C -b FileNation.c -mbg off
- Link -w -t STAK -c WILD -rt XFCN=614 ∂
- -m ENTRYPOINT ∂
- -sg FileNation ∂
- FileNation.c.o ∂
- "{Libraries}HyperXLib.o" ∂
- "{Libraries}Runtime.o" ∂
- "{Libraries}Interface.o" ∂
- "{CLibraries}StdCLib.o" ∂
- -o "teststack"
- */
-
- #include <Types.h>
- #include <Resources.h>
- #include <string.h>
- #include <Memory.h>
- #include <Packages.h>
- #include "HyperXCmd.h"
-
- #define NULL (long) 0
- #define NIL (long) 0
-
- #define kNumParams 1
- #define FALSE 0
- #define TRUE 1
-
-
- /* prototypes */
- void ErrorBack(XCmdPtr paramPtr, char *message);
- void MoveLockParams ( XCmdPtr paramPtr, short paramCount );
- void UnlockParams ( XCmdPtr paramPtr, short paramCount );
-
-
-
- pascal void EntryPoint(XCmdPtr paramPtr)
- {
- /* variable declarations */
- short nationCode;
- Str255 nationStr;
- short i,j;
- char volName[34];
- char ppathName[260];
- short vRefNum;
- Boolean* orgResLoadPtr;
- Boolean orgResLoad;
- short orgResFile;
- short theResFile;
- short theError;
- VersRecHndl versHandle;
-
-
- /* move high and lock the parameters. */
- MoveLockParams(paramPtr, paramPtr->paramCount);
-
- /* check for copyright or syntax help request */
- if (!strcmp( (char*)*paramPtr->params[0], "!") ) {
- ErrorBack(paramPtr, "v1.0, ©1991 Apple Computer, Inc.; by Mike Byrne");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- } else if (!strcmp ( (char*)*paramPtr->params[0], "?") ) {
- ErrorBack(paramPtr, "FileNation syntax is 'FileNation(<pathname>'");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- }
-
- /* not a copyright or help request. */
- /* check for correct number of parameters */
- if (paramPtr->paramCount != kNumParams) {
- ErrorBack(paramPtr, "Error: FileNation syntax is 'FileNation(<pathname>)'");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- }
-
- /* extract the volume name from the handle, copy to a pas string,
- and get the volume reference number of the volume */
- for (i=0; ((*(paramPtr->params[0]))[i] != ':' && (i < 33)); i++)
- { volName[i] = (*(paramPtr->params[0]))[i]; }
- volName[i] = ':';
- volName[i+1] = '\0';
- c2pstr(volName);
- vRefNum = 0;
-
- if (SetVol(volName, vRefNum) != noErr) { // toolbox
- ErrorBack(paramPtr, "Error: Could not set the default volume");
- UnlockParams(paramPtr, kNumParams);
- return;
- }
-
- if (GetVol(&volName, &vRefNum) != noErr) { // toolbox
- ErrorBack(paramPtr, "Error: Could not find the volume requested.");
- UnlockParams(paramPtr, kNumParams);
- return;
- }
-
- /* now, copy the rest of the pathname to the partial pathname and convert it. */
- for (j=i; (j <= strlen((*(paramPtr->params[0]))) && (j < 300)); j++)
- { ppathName[j-i] = (*(paramPtr->params[0]))[j]; }
- c2pstr(ppathName);
-
-
- /* First, save the old state. */
- orgResFile = CurResFile();
- (long) orgResLoadPtr = 0xA5E;
- if (*orgResLoadPtr) {
- orgResLoad = TRUE;
- } else {
- orgResLoad = FALSE;
- }
-
- /* flip the ResLoad (for efficiency), and open the file. */
- SetResLoad(FALSE);
- theResFile = OpenRFPerm(ppathName, vRefNum, fsRdPerm);
- theError = ResError();
-
- /* check if there is even a resource fork. */
- if (theError == -39) {
- SetResLoad(orgResLoad);
- CloseResFile(theResFile);
- ErrorBack(paramPtr, "Error: The file has no resource fork.");
- UnlockParams(paramPtr, kNumParams);
- return;
- }
-
- /* check for anything else */
- if (theError != noErr) {
- SetResLoad(orgResLoad);
- CloseResFile(theResFile);
- ErrorBack(paramPtr, "Error: The file could not be found or opened.");
- UnlockParams(paramPtr, kNumParams);
- return;
- }
-
-
- /* okay, we're cool for now. Set up the resload and resfile. */
- SetResLoad(TRUE);
- UseResFile(theResFile);
-
- /* check for 'vers' resources */
- if (Count1Resources('vers') == 0) {
- UseResFile(orgResFile);
- SetResLoad(orgResLoad);
- CloseResFile(theResFile);
- ErrorBack(paramPtr, "Error: The file has no 'vers' resource.");
- UnlockParams(paramPtr, kNumParams);
- return;
- }
-
- /* we have at least one vers resource. Try to load vers resource ID 1 first. If that
- gives us a NIL or an error, try for vers resource ID 2. If THAT yields NIL or an
- error, give up and go home. */
- versHandle = (VersRecHndl) Get1Resource('vers',1);
- theError = ResError();
- if ( (versHandle == NIL) || (theError != noErr) ) {
- versHandle = (VersRecHndl) Get1Resource('vers',2);
- theError = ResError();
- if ( (versHandle == NIL) || (theError != noErr) ) {
- UseResFile(orgResFile);
- SetResLoad(orgResLoad);
- CloseResFile(theResFile);
- ErrorBack(paramPtr, "Error: The 'vers' resource could not be opened.");
- UnlockParams(paramPtr, kNumParams);
- return;
- }
- }
-
- /* get the nation code out. */
- nationCode = (**versHandle).countryCode;
- NumToString((long) nationCode, nationStr);
- p2cstr(nationStr);
-
-
- /* clean up, then go home. */
- SetResLoad(orgResLoad);
- UseResFile(orgResFile);
- CloseResFile(theResFile);
- ErrorBack(paramPtr, nationStr);
- UnlockParams(paramPtr, kNumParams);
- return;
-
- }
-
-
-
-
-
-
- /* allocate and load up paramPtr->returnValue with a string
- -------------------------------------------------------- */
- void ErrorBack(XCmdPtr paramPtr, char *message)
- {
- Handle mesHnd;
-
- /*
- Allocate space for an error message.
- Copy the string into it.
- Return the handle to HyperCard.
- */
- mesHnd = NewHandle((long)(strlen(message)+1));
- if (mesHnd == nil) return;
- strcpy((char *)*mesHnd,message);
- paramPtr->returnValue = mesHnd;
- }
-
-
-
- /* move high and lock down all parameters
- ----------------------------------------------------------------------- */
- void MoveLockParams ( XCmdPtr paramPtr, short paramCount )
- {
- short i;
-
- for(i=0; i <= paramCount-1; i++)
- {
- MoveHHi(paramPtr->params[i]);
- HLock(paramPtr->params[i]);
- }
- }
-
-
-
-
- /* unlock all parameter handles in the XCmdBlock
- --------------------------------------------- */
- void UnlockParams ( XCmdPtr paramPtr, short paramCount )
- { short i;
-
- for(i=0; i <= paramCount-1; i++)
- { HUnlock(paramPtr->params[i]);}
- }
-